package demo.jdbc.model;

import java.util.Date;
import java.util.List;
import java.io.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import demo.jdbc.dao.BookmarkDAO;


public class Bookmark {
	
	/**
	 * An id from database; included because of technical reasons, 
	 * not because bookmarks really need id's. 
	 */
	protected long id;
	
	/**
	 * The URL address itself as a string
	 */
	protected String url;
	
	/**
	 * The title of the resource
	 */
	protected String title;
	
	/**
	 * The fingerprint of the Web page content
	 * (used to discover different URLs that lead
	 * to identical documents)
	 */
	protected long checksum;
	
	/**
	 * The date when the resource was last changed
	 */
	protected Date lastModified;
	
	/**
	 * The content of the Web page
	 */
	protected String content;
	
	
	/**
	 * User-given description of the Web page
	 */
	protected String description;

	/**
	 * User-provided "tags" (short keywords or characteristic
	 * strings) identifying the resource. 
	 */
	protected List<Tag> tags;
	
	/**
	 *  User-provided picture (attachment).
	 */
	protected byte[] attachment = null;;

	public Bookmark() {
	}
	
	public Bookmark(String url, String title) {
		this.url = url;
		this.title = title;

	}

	public Bookmark(String url, String title, String filename) {
		this.url = url;
		this.title = title;
		setFileAttachment(filename);
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public List<Tag> getTags() {
		return tags;
	}

	public void setTags(List<Tag> tags) {
		this.tags = tags;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public long getChecksum() {
		return checksum;
	}

	public void setChecksum(long checksum) {
		this.checksum = checksum;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getLastModified() {
		return lastModified;
	}

	public void setLastModified(Date lastModified) {
		this.lastModified = lastModified;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public byte[] getAttachment() {
		return attachment;
	}

	public void setAttachment(byte[] attachment) {
		this.attachment = attachment;
	}
	

	/**
	 * log.error("Error : File not found - " + filename);
	 * Read attachment from File (filename).
	 */
	public void setFileAttachment(String filename) {
		try {
			File file = new File(filename);
			FileInputStream fis = new FileInputStream(file);
			
			byte[] result = new byte[(int) file.length()];
			fis.read(result);
			this.attachment = result;
			fis.close();
		} catch (FileNotFoundException e) {
			Log log = LogFactory.getLog(Bookmark.class);
			log.error("Error : File not found - " + filename);
			this.attachment = null;
		} catch (IOException e) {
			Log log = LogFactory.getLog(Bookmark.class);
			log.error("Error : Error reading file - " + filename);
			this.attachment = null;
		} catch (Exception e) {
		}
	}
	
	
	/**
	 * 
	 * Write attachment to File (filename).
	 */
	public boolean getFileAttachment(String filename) {
		Log log = LogFactory.getLog(Bookmark.class);
		boolean result = true;
		
		if (this.attachment != null) {
			try {
				File file = new File(filename);
				FileOutputStream fos = new FileOutputStream(file);
					
				fos.write(this.attachment);
				
				fos.close();		
			} catch (FileNotFoundException e) {
				result = false;
				log.error("Error : File not found - " + filename);
			} catch (IOException e) {
				result = false;
				log.error("Error : Error writing file - " + filename);
			}
		} else {
			result = false;
		}			
		return result;
	}

	@Override
	public String toString() {
		return "<" + url + ">";
	}

	
}
